﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using DZH.TextSearch.BLL;
using DZH.TextSearch.Model;
using System.Web.UI.HtmlControls;

namespace DZH.TextSearch.Web
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string key = Request["key"];
                if (string.IsNullOrEmpty(key))
                {   
                    Response.Redirect("Default.aspx");
                }
                else 
                {
                    key = key.Trim();
                    string c2 = Request["c2"].Trim();
                    if (!string.IsNullOrEmpty(c2))
                    {
                        cboCategory.SelectedValue = c2;
                    }

                    tbxSearch.Text = key;

                    btnOK_Click(null, null);
                }
            }
        }

        protected void rptResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            SearchResultModel row = e.Item.DataItem as SearchResultModel;

            if (row != null)
            {
                Literal litTitle = e.Item.FindControl("litTitle") as Literal;
                litTitle.Text = row.C3;


                Literal litContent = e.Item.FindControl("litContent") as Literal;
                litContent.Text = row.C15c;

                Literal litFrom = e.Item.FindControl("litFrom") as Literal;
                litFrom.Text = row.C8;

                Literal litDateTime = e.Item.FindControl("litDateTime") as Literal;
                litDateTime.Text = DateTime.ParseExact(row.C6.ToString(), "yyyyMMddHHmmss", null).ToShortDateString();

                Literal litScore = e.Item.FindControl("litScore") as Literal;
                litScore.Text = row.Score.ToString();
            }
        }

        private int GetCurrPageID
        {
            get
            {
                if (string.IsNullOrEmpty(Request["p"]))
                {
                    return 0;
                }

                return int.Parse(Request["p"]);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {   
            string key = tbxSearch.Text.Trim();
            if(string.IsNullOrEmpty(key))
            {
                return;
            }

            PagerModel pager = new PagerModel();
            pager.CurrPageID = GetCurrPageID;

            IList<SearchResultModel> table = Query.QueryBy(cboCategory.SelectedValue, key, ref pager);
            rptResult.DataSource = table;
            rptResult.DataBind();

            PagerBind(pager);
            litCount.Text = pager.DataCount.ToString();
        }

        private void PagerBind(PagerModel pager)
        {
            phPages.Controls.Clear();

            if (pager.DataCount <= 0)
            {
                lbtFirst.Visible = false;
                lbtLast.Visible = false;
                return;
            }

            lbtFirst.Visible = true;
            lbtLast.Visible = true;
            
            int iPageID = GetCurrPageID;
            int pageCount = pager.PageCount;

            lbtFirst.Attributes["href"] = string.Format("./Search.aspx?key={0}&c2={1}&p={2}", tbxSearch.Text.Trim(), cboCategory.SelectedValue, 0);

            int max = iPageID <= 5 ? (pageCount >= 10 ? 10 : pageCount) : (iPageID + 5 >= pageCount ? pageCount : iPageID + 5);

            for (int i = iPageID <= 5 ? 0 : iPageID - 5; i < max; ++i)
            {
                HtmlAnchor item = new HtmlAnchor();

                if (iPageID != i)
                {
                    item.InnerText = (i + 1).ToString();
                    item.Attributes["href"] = string.Format("./Search.aspx?key={0}&c2={1}&p={2}", tbxSearch.Text.Trim(), cboCategory.SelectedValue, i);
                }
                else
                {
                    item.InnerHtml = "<font color='black' size='6'>" + (i + 1).ToString() + "</font>";
                }

                phPages.Controls.Add(item);
            }

            lbtLast.Attributes["href"] = string.Format("./Search.aspx?key={0}&c2={1}&p={2}", tbxSearch.Text.Trim(), cboCategory.SelectedValue, pager.PageCount - 1);
        }
    }
}